package http;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.shishi.tech.paipai.application.BaseApplication;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import log.LogManager;
import utils.PhoneUtil;
import utils.common.ToastUtil;

/**
 * Created by Administrator on 2015/8/28.
 */
public class AsyncHttpUtil {

    private static final String TAG = "AsyncHttpUtil";

    private static AsyncHttpClient client = new AsyncHttpClient();

    private static final int DEFAULT_SOCKET_TIMEOUT = 30 * 1000;

    static {
        client.setResponseTimeout(DEFAULT_SOCKET_TIMEOUT);
        if (ApiUtil.isDebug()) {
            client.setLoggingEnabled(true);
        }
        client.setUserAgent("Android-makeba-v"+ BaseApplication.getInstance().versionName);
    }

    public static void post(Context context, String url, RequestParams param, AsyncHttpResponseHandler handler) {
        if (PhoneUtil.isConnectNet(context)) {
            LogManager.getLogger().l("HTTP 请求URL=======>" + getAbsoluteUrl(url));
            client.post(context, getAbsoluteUrl(url), param, handler);
        } else {
            ToastUtil.showMsg("网络已断开");
            handler.sendFinishMessage();
            handler.onCancel();
        }
    }

    public static void post(Context context, String url, RequestParams param, Map<String, String> clientHeaderMap, AsyncHttpResponseHandler handler) {
        if (PhoneUtil.isConnectNet(context)) {
            LogManager.getLogger().l("HTTP 请求URL=======>" + getAbsoluteUrl(url));
            if (clientHeaderMap != null){
                for (String header : clientHeaderMap.keySet()) {
                    try {
                        client.addHeader(header, URLEncoder.encode(URLEncoder.encode(clientHeaderMap.get(header),"UTF-8"),"UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
            client.post(context, getAbsoluteUrl(url), param,handler);
        } else {
            ToastUtil.showMsg("网络已断开");
            handler.sendFinishMessage();
            handler.onCancel();
        }
    }

    /**
     * post IM
     *
     * @param context
     * @param url
     * @param param
     * @param handler
     */
    public static void im(Context context, String url, RequestParams param, AsyncHttpResponseHandler handler) {
        if (PhoneUtil.isConnectNet(context)) {
            LogManager.getLogger().l(getAbsoluteUrl(url));
            client.get(context, getIMAbsoluteUrl(url), param, handler);
        } else {
            ToastUtil.showMsg("网络已断开");
            handler.sendFinishMessage();
            handler.onCancel();
        }
    }

    public static void get(Context context, String url, RequestParams param, AsyncHttpResponseHandler handler) {
        if (PhoneUtil.isConnectNet(context)) {
            LogManager.getLogger().l(getAbsoluteUrl(url));
            client.get(context, getAbsoluteUrl(url), param, handler);
        } else {
            ToastUtil.showMsg("网络已断开");
            handler.sendFinishMessage();
            handler.onCancel();
        }
    }

    public static void get(Context context, String uString, AsyncHttpResponseHandler handler) {
        if (PhoneUtil.isConnectNet(context)) {
            LogManager.getLogger().l(uString);
            client.get(uString, handler);
        } else {
            ToastUtil.showMsg("网络已断开");
            handler.sendFinishMessage();
            handler.onCancel();
        }
    }

    /**
     * 获取骑牛文件
     */
    public static void getQiNiu(Context context, String url, AsyncHttpResponseHandler handler, Object... objs) {
        if (PhoneUtil.isConnectNet(context)) {
            client.get(context, url, null, handler);
        } else {
            ToastUtil.showMsg("网络已断开");
        }
    }


    private static String getAbsoluteUrl(String url) {
        return ApiUtil.getApiServerPath() + url;
    }

    private static String getIMAbsoluteUrl(String url) {
        return ApiUtil.getApiIMServerPath() + url;
    }
}
